package cinema.dao;

import java.sql.SQLException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

import cinema.model.Actors;
import cinema.model.Movies;
import cinema.model.Users;
import cinema.utils.HibernateUtil;

public class ActorsDaoHibernate extends Initialization implements ActorsDao {
	
	
	@Override
	public Actors getActorsbyID(int ID){

		try{
			
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			
			session.beginTransaction();
			
			List<Actors> result = session.createQuery("from Actors where ID = ?").setInteger(0, ID).list();

			 if (result.size() > 0) {
		            
		        	ListIterator<Actors> it = result.listIterator();
		        	
		        	Actors act = (Actors) it.next();
		            
		            Iterator<Users> iter = act.getUsers().iterator();
		            
					Set<Users> users = new HashSet<Users>();
					
			        				
					while(iter.hasNext()){
						Users user = iter.next();
						users.add(user);
					}
					
					act.setUsers(users);
		            

					Iterator<Movies> iter2 = act.getMovies().iterator();
		            
					Set<Movies> movies = new HashSet<Movies>();
					
			        				
					while(iter2.hasNext()){
						Movies movie = iter2.next();
						movies.add(movie);
					}
					
					act.setMovies(movies);
					
		            if (! HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().wasCommitted())
			        	session.getTransaction().commit();
		            
		            return act;
		        }
		        else{
		        	
		        	if (! HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().wasCommitted())
			        	session.getTransaction().commit();
		        	
		        	return null;
		        }
			
		} catch (Exception e){
            return null;
		}
	}
	
	
	@Override
	public boolean saveActors(Actors actorsObject) {
		try{
			
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			
			session.beginTransaction();
			
			session.saveOrUpdate(actorsObject);
			
			if (! HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().wasCommitted())
	        	session.getTransaction().commit();
			
			return true;
			
		} catch (Exception e){
            return false;
		}
		
	}
	
	@Override
	public boolean deleteActors(Actors actorsObject){

		try{
			
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			
			session.beginTransaction();
			
			session.delete(actorsObject);
			
			if (! HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().wasCommitted())
	        	session.getTransaction().commit();
			
			return true;
			
		} catch (Exception e){
            return false;
		}
	}
	@Override
	public ListIterator<Actors> getActors() {

		try{
			
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			
			session.beginTransaction();
			
			List<Actors> result = session.createCriteria(Actors.class).list();
			
			for(Actors act : result ){
				
				Iterator<Users> iter = act.getUsers().iterator();
	            
				Set<Users> users = new HashSet<Users>();
				
		        				
				while(iter.hasNext()){
					Users user = iter.next();
					users.add(user);
				}
				
				act.setUsers(users);
	            

				Iterator<Movies> iter2 = act.getMovies().iterator();
	            
				Set<Movies> movies = new HashSet<Movies>();
				
		        				
				while(iter2.hasNext()){
					Movies movie = iter2.next();
					movies.add(movie);
				}
				
				act.setMovies(movies);
				
	            
				
			}
			
			if (! HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().wasCommitted())
	        	session.getTransaction().commit();
			
			ListIterator<Actors> it = result.listIterator();
			
			return it;
			
		} catch (Exception e){
            return null;
		}
	}
}